package com.ywjh.inter.servlet.doc;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Timestamp;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ywjh.inter.entity.BloodGlucoseEntity;
import com.ywjh.inter.entity.BloodPressureEntity;
import com.ywjh.inter.entity.CholesterolEntity;
import com.ywjh.inter.entity.EquBindEntity;
import com.ywjh.inter.entity.UricAcidEntity;
import com.ywjh.inter.entity.UsersEntity;
import com.ywjh.inter.entity.UsersHXEntity;
import com.ywjh.inter.service.DoctorService;
import com.ywjh.inter.service.EquBindService;
import com.ywjh.inter.service.PatientDataService;
import com.ywjh.inter.service.UsersService;
import com.ywjh.inter.utils.StrUtil;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

public class Stat extends HttpServlet {
	/**
	 * 
	 */
	private static final long serialVersionUID = -5518294686278556944L;

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		resp.setCharacterEncoding("UTF-8");
		resp.setContentType("text/html; charset=utf-8");

		JSONObject postData = StrUtil.getPostJSON(req);
		String act = postData.getString("act");
		JSONObject data = postData.getJSONObject("data");
		JSONObject json = new JSONObject();
		int r = 0;
		String e = "";
		switch (act) {
		case "stat1":
			int doc_id = data.getInt("doc_id");
			UsersHXEntity hx_user = UsersService.getInstance().findHXUserByUserId(doc_id);
			if (hx_user == null) {
				e = "hx用户未找到";
			} else {
				r=1;
				String today = StrUtil.getYYYYMMDD(new Timestamp(System.currentTimeMillis()));
				List<Integer> friendList = DoctorService.getInstance().getPatientIdList(doc_id);
				int total_patient = friendList.size();
				int total_glu_check=0;
				int total_pre_check=0;
				int total_ua_check = 0;
				int total_chol_check = 0;
				int bind_bglu = 0;
				int bind_bpre = 0;
				int bind_ua = 0;
				int bind_chol = 0;
				int today_bind=0;
				int today_patient = 0;
				int today_check = 0;
				int today_glu_check = 0;
				int today_pre_check = 0;
				int today_ua_check = 0;
				int today_chol_check = 0;
				int day_7_glu_check = 0;
				int day_7_pre_check = 0;
				int day_7_ua_check = 0;
				int day_7_chol_check = 0;
				System.out.println("today="+today);
				Set<Integer> bindSet=new HashSet<>();
				for (int fid : friendList) {
					System.out.println("friend:"+fid);
					List<EquBindEntity> bindList = EquBindService.getInstance().listByUserId(fid);
					for (EquBindEntity equBindEntity : bindList) {
						if (equBindEntity.getType() == 1) {
							bind_bglu++;
						}
						if (equBindEntity.getType() == 2) {
							bind_bpre++;
						}
						if (equBindEntity.getType() == 4) {
							bind_ua++;
						}
						if (equBindEntity.getType() == 5) {
							bind_chol++;
						}
						bindSet.add(equBindEntity.getUser_id());
					}
					UsersEntity friendEntity = UsersService.getInstance().findUserById(fid);
					
					if (friendEntity != null) {

						if (today.equals(StrUtil.getYYYYMMDD(friendEntity.getRegister_time()))) {
							today_patient++;
						}
					}
					List<BloodGlucoseEntity> gluList = PatientDataService.getInstance().listBloodGlucoseByTime(today, today, fid);
					List<BloodPressureEntity> presList = PatientDataService.getInstance().listBloodPressureByTime(today, today, fid);
					List<UricAcidEntity> uaList = PatientDataService.getInstance().ListUricAcidByTime(fid, today, today);
					List<CholesterolEntity> cholList = PatientDataService.getInstance().ListCholesterolByTime(fid, today, today);
					today_glu_check += gluList.size();
					today_pre_check += presList.size();
					today_ua_check += uaList.size();
					today_chol_check += cholList.size();
				}
				today_check = today_glu_check + today_pre_check + today_ua_check + today_chol_check;
				
				//7day stat
				String users="";
				for(int fid:friendList) {
					users+=fid+",";
				}
				if(users.endsWith(",")) {
					users=users.substring(0, users.length()-1);
				}
				total_glu_check=PatientDataService.getInstance().listBloodGlucoseByAllTime(users).size();
				total_pre_check=PatientDataService.getInstance().listBloodPressureByAllTime(users).size();
				today_bind=EquBindService.getInstance().listByTime(today,today,users).size();
				JSONArray array=new JSONArray();
				for (int i = 0; i < 7; i++) {
					String day = StrUtil
							.getYYYYMMDD(new Timestamp(System.currentTimeMillis() - (i * (24 * 60 * 60 * 1000))));
					System.out.println("day="+day);
					
					List<BloodGlucoseEntity> daygluList = PatientDataService.getInstance().listBloodGlucoseByTime(day, day, users);
					List<BloodPressureEntity> daypresList = PatientDataService.getInstance().listBloodPressureByTime(day, day, users);
					List<UricAcidEntity> dayuaList = PatientDataService.getInstance().ListUricAcidByTime(users, day, day);
					List<CholesterolEntity> daycholList = PatientDataService.getInstance().ListCholesterolByTime(users, day, day);
					
					System.out.println("p:"+day+" "+users+" daygluList:"+daygluList.size()+" daypresList:"+daypresList.size());
					day_7_glu_check += daygluList.size();
					day_7_pre_check += daypresList.size();
					day_7_ua_check += dayuaList.size();
					day_7_chol_check += daycholList.size();
					JSONObject dayObj=new JSONObject();
					dayObj.put("time", day);//日期 yyyy-mm-dd
					dayObj.put("glu_check", daygluList.size()); //血糖检测数
					dayObj.put("pre_check", daypresList.size());//血压检测数
					dayObj.put("ua_check", dayuaList.size());		//尿酸检测数
					dayObj.put("chol_check", daycholList.size());	//总胆固醇检测数
					array.add(dayObj);
				}
				
				JSONObject d=new JSONObject();
				d.put("total_patient", total_patient);//好友总数
				d.put("total_glu_check", total_glu_check);
				d.put("total_pre_check",total_pre_check);
				d.put("total_ua_check", total_ua_check);
				d.put("total_chol_check", total_chol_check);
				d.put("bind_patient", bindSet.size());//绑定患者人数
				d.put("bind_bglu", bind_bglu);//绑定血糖设备数
				d.put("bind_bpre", bind_bpre);//绑定血压设备数
				d.put("bind_ua", bind_ua);//绑定尿酸设备数
				d.put("bind_chol", bind_chol);//绑定总胆固醇设备数
				d.put("today_bind", today_bind);//今日绑定数
				d.put("today_patient", today_patient);//今日注册患者好友数
				d.put("today_check", today_check);//今日检测总数
				d.put("today_glu_check", today_glu_check);//今日检测血糖数
				d.put("today_pre_check", today_pre_check);//今日检测血压数
				d.put("today_ua_check", today_ua_check);//今日检测尿酸数
				d.put("today_chol_check", today_chol_check);//今日检测总胆固醇数
				d.put("day_7_glu_check", day_7_glu_check);//7日检测血压数
				d.put("day_7_pre_check", day_7_pre_check);//7日检测血压数
				d.put("day_7_ua_check", day_7_ua_check);//7日检测尿酸数
				d.put("day_7_chol_check", day_7_chol_check);//7日检测总胆固醇数
				d.put("day_7_array", array);
				json.put("d", d);
			}
			break;
		}
		json.put("r", r);
		json.put("e", e);
		PrintWriter pw = resp.getWriter();
		pw.write(json.toString());
		pw.flush();
		pw.close();
	}

}
